package tr3dtris.game.persistence;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.Serializable;
import java.util.Collections;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.ho.yaml.Yaml;

/**
 *
 * @author Luciano
 */
public class RecordArray implements Serializable
{
    private static final String FILE = "data/records.yml";
    private Vector<Record> records;

    public RecordArray()
    {
        this.records = new Vector<Record>();
    }

    public static RecordArray load()
    {
        RecordArray r = null;
        try {
            r = (RecordArray) Yaml.load(new File(FILE));
        } catch (FileNotFoundException ex) {
            Logger.getLogger(RecordArray.class.getName()).log(Level.SEVERE, null, ex);
        }
        return r;
    }

    public void save()
    {
        try {
            Yaml.dump(this, new File(FILE));
        } catch (FileNotFoundException ex) {
            Logger.getLogger(RecordArray.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * @return the records
     */
    public Vector<Record> getRecords() {
        return records;
    }

    /**
     * @param records the records to set
     */
    public void setRecords(Vector<Record> records) {
        this.records = records;
    }

    public void sort()
    {
        Collections.sort(this.records);
    }

    public void clear()
    {
        this.records = new Vector<Record>();
        this.save();
    }

    public static void add(Record record)
    {
        RecordArray r = RecordArray.load();
        r.records.add(record);
        r.sort();
        r.save();
    }


}
